using System;
using System.Collections.Generic;
using com.youzu.warh.protocol;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.Pool;

namespace Yoozoo.Gameplay.Liberty
{
    public class BaseThrowable : MonoBehaviour
    {
        // 子弹ID
        [SerializeField]
        protected int _BulletId = 0;
        // 武器配置
        protected PbWeaponDataConfig _WeaponDataConfig;
        // 子弹配置
        protected PbWeaponBulletDataConfig _WeaponBulletDataConfig;
        // 子弹拥有者
        [SerializeField]
        protected AIEntity _aiEntity;
        // 战斗代理
        protected BattleAgent _battleAgent;
        
        // 子弹生命周期计时器
        protected float _LifeTimer;
        // 子弹触发的元素
        protected HashSet<int> _triggerElementIndices = new HashSet<int>();

        public AIEntity Owner => _aiEntity;

        public void InitBullet(PbWeaponDataConfig weaponDataConfig, PbWeaponBulletDataConfig weaponBulletDataConfig, AIEntity aiEntity)
        {
            _WeaponDataConfig = weaponDataConfig;
            _WeaponBulletDataConfig = weaponBulletDataConfig;
            _BulletId = weaponBulletDataConfig.bullet_id;
            _aiEntity = aiEntity;
            _battleAgent = aiEntity.battleAgent;

            _LifeTimer = 0;
            _triggerElementIndices.Clear();

            OnInitComplete();
        }
        
        protected void DestroyBullet()
        {
            LibertyPoolMgr.GetInstance().RecycleEffect(_WeaponBulletDataConfig.model, transform);
        }

        protected virtual void OnDestroy() { }

        protected virtual void Update()
        {
            _LifeTimer += Time.deltaTime;

            if (_WeaponBulletDataConfig.duration > 0)
            {
                if (_LifeTimer >= _WeaponBulletDataConfig.duration)
                {
                    this.DestroyBullet();
                }
            }

        }

        protected virtual void OnInitComplete()
        {
            
        }

    }
}